package com.fernandomartines.dbcheck.logger;

import java.awt.Rectangle;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

public class LoggerOutputStream  extends OutputStream {

    private StringBuilder text = new StringBuilder();
    private StringBuilder line = new StringBuilder();
    private JTextArea memo;

    public LoggerOutputStream(JTextArea memo) {
        this.memo = memo;
    }

    @Override
    public void write(int b) throws IOException {
        line.append((char) b);

        if (b == 10) {
            String s = line.toString();

            if (s.contains("@clear")) {
                text = new StringBuilder();
            } else {
                text.append(s);
            }

            if (s.contains("\n")) {
                memo.setText(text.toString());
                memo.repaint();
                memo.scrollRectToVisible(new Rectangle(0, 2000000000, 1, 1));
            }

            line = new StringBuilder();
        }
    }
}
